package top.roadvast.rabbitmq.provider.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.test.context.SpringBootTest;
import top.roadvast.rabbitmq.utils.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

// 生产者
@Slf4j
@SpringBootTest
public class WorkSend {
    private final static String QUEUE_NAME = "work_queue";

    public static void main(String[] arg) {
        // 获取到连接
        // 获取通道
        try (Connection connection = ConnectionUtil.getConnection();
             Channel channel = connection.createChannel();) {
            // 声明队列
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            // 循环发布任务
            for (int i = 0; i < 50; i++) {
                // 消息内容
                String message = "task .. " + i;
                channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
                System.out.println(" [x] Sent '" + message + "'");
                try {
                    Thread.sleep(i * 2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
        }
    }
}